<template>
    <div class='approval'>
      <el-card class="box-card" style="margin-top: 110px;overflow:scroll;">
        <div class="head-title2">
          <div class='title'>
            {{$t('achievements.title1')}}
          </div>
          <div class="info">
            <el-tooltip placement="left">
              <div slot="content">
                {{define}}
              </div>
              <div class="btns"></div>
            </el-tooltip>
          </div>
        </div>
        <div class='menu-search'> 
            <div class='menu-title'>
              <!-- 选择时间 -->
              <div class='searchItem searchItem-date'>
                <span>{{$t('ApprReport.choiceTime')}}</span>
                <el-date-picker
                  size='small'
                  v-model="listQuery.startDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  :placeholder="$t('rule.beginDate')"
                ></el-date-picker>
                <b>{{$t('approval.go')}}</b>
                <el-date-picker
                  size='small'
                  v-model="listQuery.endDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  :placeholder="$t('rule.endDate')"
                ></el-date-picker>
              </div>
              <!-- 是否续贷 -->
              <div class='searchItem'>
                <span>{{$t('orderQuery.ctnLoan')}}</span>
                <el-select v-model='listQuery.refinanceType' size='small'>
                  <el-option
                  v-for='item in conLoan'
                  :key='item.value'
                  :label='item.label'
                  :value='item.value'>
                  </el-option>
                </el-select>
              </div>
            </div>
            <div class='menu-title-btn'>
                <button type='button' @click='search'>
                    {{$t('btn.query')}}
                </button>
                <button type='button' @click='reset'>
                    {{$t('btn.reset')}}
                </button>
                <button type='button' @click="handleDownload" :disabled="expDis" v-if="DailyStatistics_btn_export">
                    {{$t('btn.export')}}
                </button>
            </div>
        </div>
      </el-card>
        <el-card class='box-card' style="margin-top: 10px;margin-bottom: 20px;">
          <div class='title'>
              {{$t('DailyStatistics.ListDetails')}}
          </div>
          <el-table
              :sum-text="$t('ApprReport.total')"
              v-loading="listLoading"
              ref='multipleTable'
              :data='tableData'
              tooltip-effect='dark'
              style='width: 100%; margin-top: 5px;'
              @selection-change='handleSelectionChange'
              border
              size='mini'>
              <!-- 时间 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='amountDate'
              width="140"
              :label="$t('approvalDetail.riskTime')">
              </el-table-column>
              <!-- 渠道 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='appName'
              width="140"
              :label="$t('productManage.channels')">
              </el-table-column>
              <!-- 注册数 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='registryNum'
              width="100"
              :label="$t('DailyStatistics.RegisterNum')">
              </el-table-column>
              <!-- 申请数 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='loanApplyNum'
              width="100"
              :label="$t('DailyStatistics.ApplyNum')">
              </el-table-column>
              <!-- 放款数 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='lendNum'
              width="100"
              :label="$t('DailyStatistics.LoanNum')">
              </el-table-column>
              <!-- 放款本金 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='approvedAmtTotal'
              width="100"
              :label="$t('DailyStatistics.LoanPrincipal')">
              </el-table-column>
              <!-- 实际放款金额 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='lendRealCost'
              width="140"
              :label="$t('DailyStatistics.ActualLoanAmt')">
              </el-table-column>
              <!-- 还款笔数 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='repayNumTotal'
              width="100"
              :label="$t('DailyStatistics.RepayAmount')">
              </el-table-column>
              <!-- 还款金额 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='repayAmt'
              width="140"
              :label="$t('DailyStatistics.RepayAmt')">
              </el-table-column>
              <!-- 当日应还金额 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='contractRepayAmt'
              width="140"
              :label="$t('DailyStatistics.SameDayShouldReturnAmt')">
              </el-table-column>
              <!-- 还款结清数 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='repaySettleNum'
              width="140"
              :label="$t('DailyStatistics.RepaySettleNum')">
              </el-table-column>
              <!-- 到期还款结清数 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='repayExpireNum'
              width="140"
              :label="$t('DailyStatistics.ExpireRepaySettleNum')">
              </el-table-column>
              <!-- 提前还款结清数 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='repayAdvancedNum'
              width="140"
              :label="$t('DailyStatistics.AdvanceRepaySettleNum')">
              </el-table-column>
              <!-- 逾期还款结清数 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='repayOverdueNum'
              width="140"
              :label="$t('DailyStatistics.OvdRepaySettleNum')">
              </el-table-column>
              <!-- 还款结清金额 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='repaySettlePrice'
              width="140"
              :label="$t('DailyStatistics.RepaySettleAmt')">
              </el-table-column>
              <!-- 到期还款结清金额 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='repayExpirePrice'
              width="140"
              :label="$t('DailyStatistics.ExpireRepaySettleAmt')">
              </el-table-column>
              <!-- 提前还款结清金额 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='repayAdvancedPrice'
              width="140"
              :label="$t('DailyStatistics.AdvanceRepaySettleAmt')">
              </el-table-column>
              <!-- 逾期还款结清金额 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='repayOverduePrice'
              width="140"
              :label="$t('DailyStatistics.OvdRepaySettleAmt')">
              </el-table-column>
              <!-- 当日应还数 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='todayLoanNum'
              width="140"
              :label="$t('DailyStatistics.SameDayShouldReturnNum')">
              </el-table-column>
              <!-- 当日已还数 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='todayYetNum'
              width="140"
              :label="$t('DailyStatistics.SameDayAlreadyRepaidNum')">
              </el-table-column>
              <!-- 当日未还数 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='todayNoneNum'
              width="140"
              :label="$t('DailyStatistics.SameDayNotReturnedNum')">
              </el-table-column>
              <!-- 当日首逾率 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='todayOverduePercentage'
              width="140"
              :label="$t('DailyStatistics.SameDayFirstRate')">
              </el-table-column>
              <!-- 当日实际应还金额 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='todayLoanPrice'
              width="140"
              :label="$t('DailyStatistics.TodayActualShouldRtAmt')">
              </el-table-column>
              <!-- 当日已还金额 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='todayYetPrice'
              width="140"
              :label="$t('DailyStatistics.SameDayAlreadyRepaidAmt')">
              </el-table-column>
              <!-- 当日未还金额 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='todayNonePrice'
              width="140"
              :label="$t('DailyStatistics.SameDayNotReturnedAmt')">
              </el-table-column>
              <!-- 当日金额首逾率 -->
              <el-table-column
              header-align='center'
              align='center'
              prop='todayPriceOverduePercentage'
              width="140"
              :label="$t('DailyStatistics.SameDayFirstRateAmt')">
              </el-table-column> -->
          </el-table>
          <div class="pagination-container">
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listQuery.page"
            :page-sizes="[10, 30, 50]"
            :page-size="listQuery.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
            </el-pagination>
          </div>
        </el-card>
    </div>
</template>

<script>
import { mapGetters } from "vuex";
import { formatAmt } from "utils/index";
import { page, exportList } from 'api/reportForm/DailyStatistics/index'
export default {
  data() {
    return {
      define: '',
      changeDate: [new Date(), new Date()],
      DailyStatistics_btn_export: false,
      listLoading: false,
      total: null,
      expDis: true,
      listQuery: {
        limit: 10,
        page: 1,
        startDate: '',
        endDate: '',
        refinanceType: 1
      },
      tableData: [],
      multipleSelection: [],
      conLoan: [
        {
          value: 1,
          label: this.$t('rule.all')
        },
        {
          value: 2,
          label: this.$t('approvalDetail.no')
        },
        {
          value: 3,
          label: this.$t('approvalDetail.yes')
        }
      ]
    };
  },
  computed: {
    ...mapGetters(["elements", "statisticsPage", 'getReportDefine'])
  },
  created() {
    if (this.statisticsPage.has(this.$route.path)) {
      this.listQuery = this.statisticsPage.get(this.$route.path)
    }
    this.getTime();
    this.getList();
    this.DailyStatistics_btn_export = this.elements['DailyStatistics:btn_export'];
    // 获取报表定义
    this.getReportDefine.forEach(item => {
      if (item.dictCode === 'DAILY_STATISTICS') {
        this.define = item.dictValue
      }
    })
  },
  methods: {
    getTime() {
      this.listQuery.startDate = this.GetDateStr(0);
      this.listQuery.endDate = this.GetDateStr(0);
    },
    GetDateStr(AddDayCount) {
      let dd = new Date();
      dd.setDate(dd.getDate() + AddDayCount);// 获取AddDayCount天后的日期
      let y = dd.getFullYear();
      let m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1); // 获取当前月份的日期，不足10补0
      let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
      return y + '-' + m + '-' + d;
    },
    // 点击导出
    handleDownload() {
      this.listLoading = true;
      exportList(this.listQuery)
      .then(res => {
        require.ensure([], () => {
          const { export_json_to_excel } = require('@/vendor/Export2Excel')
          const tHeader = [ this.$t('approvalDetail.riskTime'), this.$t('productManage.channels'), this.$t('DailyStatistics.RegisterNum'), this.$t('DailyStatistics.ApplyNum'), this.$t('DailyStatistics.LoanNum'), this.$t('DailyStatistics.LoanPrincipal'), this.$t('DailyStatistics.ActualLoanAmt'), this.$t('DailyStatistics.RepayAmount'), this.$t('DailyStatistics.RepayAmt'), this.$t('DailyStatistics.SameDayShouldReturnAmt'), this.$t('DailyStatistics.RepaySettleNum'), this.$t('DailyStatistics.ExpireRepaySettleNum'), this.$t('DailyStatistics.AdvanceRepaySettleNum'), this.$t('DailyStatistics.OvdRepaySettleNum'), this.$t('DailyStatistics.RepaySettleAmt'), this.$t('DailyStatistics.ExpireRepaySettleAmt'), this.$t('DailyStatistics.AdvanceRepaySettleAmt'), this.$t('DailyStatistics.OvdRepaySettleAmt'), this.$t('DailyStatistics.SameDayShouldReturnNum'), this.$t('DailyStatistics.SameDayAlreadyRepaidNum'), this.$t('DailyStatistics.SameDayNotReturnedNum'), this.$t('DailyStatistics.SameDayFirstRate'), this.$t('DailyStatistics.TodayActualShouldRtAmt'), this.$t('DailyStatistics.SameDayAlreadyRepaidAmt'), this.$t('DailyStatistics.SameDayNotReturnedAmt'), this.$t('DailyStatistics.SameDayFirstRateAmt') ];
          const filterVal = [ 'amountDate', 'appName', 'registryNum', 'loanApplyNum', 'lendNum', 'approvedAmtTotal', 'lendRealCost', 'repayNumTotal', 'repayAmt', 'contractRepayAmt', 'repaySettleNum', 'repayExpireNum', 'repayAdvancedNum', 'repayOverdueNum', 'repaySettlePrice', 'repayExpirePrice', 'repayAdvancedPrice', 'repayOverduePrice', 'todayLoanNum', 'todayYetNum', 'todayNoneNum', 'todayOverduePercentage', 'todayLoanPrice', 'todayYetPrice', 'todayNonePrice', 'todayPriceOverduePercentage' ];
          const list = res.data.rows;
          const data = this.formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, this.$t('route.DailyStatistics'))
          this.listLoading = false;
        })
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    getList() {
      this.listLoading = true;
      this.$store.dispatch('saveStatisticsPage', { path: this.$route.path, listQuery: this.listQuery });
      page(this.listQuery)
        .then(res => {
          res.data.rows.forEach(item => {
            item.lendRealCost = formatAmt(item.lendRealCost);
            item.repayAmt = formatAmt(item.repayAmt);
            item.repaySettlePrice = formatAmt(item.repaySettlePrice);
            item.repayExpirePrice = formatAmt(item.repayExpirePrice);
            item.repayAdvancedPrice = formatAmt(item.repayAdvancedPrice);
            item.repayOverduePrice = formatAmt(item.repayOverduePrice);
            item.todayLoanPrice = formatAmt(item.todayLoanPrice);
            item.todayYetPrice = formatAmt(item.todayYetPrice);
            item.todayNonePrice = formatAmt(item.todayNonePrice);
            item.approvedAmtTotal = formatAmt(item.approvedAmtTotal);
            item.contractRepayAmt = formatAmt(item.contractRepayAmt);
          })
          this.expDis = false;
          this.listLoading = false;
          this.tableData = res.data.rows;
          this.total = Number(res.data.total);
        })
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 10;
      this.listQuery.page = 1;
      this.changeDate = [new Date(), new Date()];
      this.listQuery.startDate = this.GetDateStr(0);
      this.listQuery.endDate = this.GetDateStr(0);
      this.listQuery.refinanceType = 1;
    },
    //选中的数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList()
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before{
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px  0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div:nth-child(2) {
      width: 150px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px  0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
       width: 150px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.head-title2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.btns {
  display: inline-block;
  width: 24px;
  height: 24px;
  background-size: cover;
  background-image: url("../../../assets/images/question-mark.svg");
}
</style>